# -*- coding: utf-8 -*-

'''
@project : AIGateWay
@FileName: image_util
@Author  :linych 
@Time    :2020/12/11 9:32
@Desc  : 
'''


import base64
import numpy as np

import cv2




def base64toarray(imagebase64):

    base64Image = imagebase64.replace("%2B", "+").replace("%3D", "=").replace("%2F", "/")
    img = base64.b64decode(base64Image)

    img_array = np.frombuffer(img, np.uint8)
    ori_img_array = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR)


    return ori_img_array


def image_pos(fea_x1,fea_y1,fea_w,fea_h,all_w,all_h,drop=0.2):

    p_x1 = all_w * drop
    p_x2 = all_w * (1-drop)
    p_y1 = all_h * drop
    p_y2 = all_h * (1-drop)

    fea_x2 = fea_x1 + fea_w
    fea_y2 = fea_y1 + fea_h

    # in_h = min(fea_y2, p_y2) - max(fea_y1, p_y1)
    # in_w = min(fea_x2, p_x2) - max(fea_x1, p_x1)
    #
    # inner_area = 0 if in_h < 0 or in_w < 0 else in_h * in_w
    fea_area = fea_w * fea_h

    x11 = max(fea_x1, p_x1)
    y11 = max(fea_y1, p_y1)

    x22 = min(fea_x2, p_x2)
    y22 = min(fea_y2, p_y2)

    #print(x11, y11, x22, y22)

    inner_area = max(0, (x22 - x11) * (y22 - y11))


    return  inner_area / fea_area


def image_area(w,h):
    return w*h
